package com.easy.work.push;

import android.content.Context;
import android.content.Intent;


import com.easy.work.MainActivity;
import com.easy.work.function.LoginActivity;
import com.easy.work.function.RegisterActivity;
import com.easy.work.utils.ConvertUtil;
import com.easy.work.utils.StringUtil;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 1、包含了首页的宽街按钮的跳转action
 * 2、处理推送的action
 * 3、处理本地跳转的action
 *
 * 股票推送
 jingling://openStock?code=sh600000&name=浦发银行&title=浦发银行涨停啦&intro=描述

 资讯推送(包括H5页面)
 jingling://openNews?articleId=21554&title=大中城市房价全面降温 北上深环比停涨
 &intro=大中城市房价全面降温 北上深环比停涨
 &h5=https://www.hao123.com/

 如果需要请增加就可以了，
 * 千万不用改或者删除
 *
 */
public class OpenActivityUtil {
    public static final String SCHEME = "jingling://";
    public static final String KEY_ACTION = "action";
    public static final String IS_GOHOME = "gohome";//是否返回首页
//     bsapp://login?key1=value1&key2=value2



    /**
     *
     * 为需要的activity 配置别名
     * 根据别名找到类全路径
     *
     */
    public static Map<String, String> activityMap = new HashMap<String, String>();

    //======================================================本地的action  start========================================================
    /**
     * 行情首页
     * 参数 无
     */
    public static final String ACT_MAIN = "home";
    /**
     * 登录
     * 参数 无
     */
    public static final String ACT_LOGIN = "login";

    /**
     * 注册
     * 参数 无
     */
    public static final String ACT_REGISTER = "register";
    /**
     网页html
     参数 title  url
     *
     */
    public static final String ACT_WEBVIEW = "webView";
    /**
     * 产品详情页
     */
    public static final String ACT_PRODUCT_DETAIL = "productDetail";

    //======================================================本地的action  end========================================================



    //======================================================快捷按钮的action   start========================================================
    /**
     * bs信号
     */
    public static final String ACT_BS = "bs";

    /**
     * 模拟交易
     */
    public static final String ACT_TRADE = "simulation";

    /**
     * 文字直播act
     */
    public static final String ACT_LIVE = "live";

    /**
     * 我的私信
     * "jingling://action=session"
     */
    public static final String ACT_SESSION = "session";
    /**
     * 牛人
     * "jingling://action=teacher"
     */
    public static final String ACT_TEACHER = "teacher";
    /**
     * 策略
     * "jingling://action=tactics"
     */
    public static final String ACT_TACTICS = "tactics";
    /**
     * 资讯
     * "jingling://action=news"
     */
    public static final String ACT_INDEX_NEWS = "news";
    /**
     * 活动
     * "jingling://action=activity"
     */
    public static final String ACT_LINKINFO = "activity";
    //我的关注
    public static final String ACT_MY_NOTICE = "notice";
//    "jingling://action=video"
    public static final String ACT_VIDEO = "video";

    //======================================================快捷按钮的action  end========================================================

    static {
         /*行情首页*/
        activityMap.put(ACT_MAIN, MainActivity.class.getName());
        /*登录*/
        activityMap.put(ACT_LOGIN, LoginActivity.class.getName());
        /*注册  */
        activityMap.put(ACT_REGISTER, RegisterActivity.class.getName());
//        /*webview html5网址 参数 title  url*/
//        activityMap.put(ACT_WEBVIEW, WebAct.class.getName());
//        /*bs*/
//        activityMap.put(ACT_BS, BSIndexAct.class.getName());
//        activityMap.put(ACT_LIVE, LiveAct.class.getName());
//        activityMap.put(ACT_TRADE, TradeAct.class.getName());
//        //股票详情
//        activityMap.put(ACT_PRODUCT_DETAIL, ProductDetailAct.class.getName());
//        activityMap.put(ACT_INDEX_NEWS, NewsAct.class.getName());
//        activityMap.put(ACT_LINKINFO, LinkInfoAct.class.getName());
//        activityMap.put(ACT_SESSION, MyMsgAct.class.getName());
//        activityMap.put(ACT_MY_NOTICE, MyNoticeAct.class.getName());
//        activityMap.put(ACT_TEACHER, TeacherAct.class.getName());
//        activityMap.put(ACT_TACTICS, TactAct.class.getName());
//        activityMap.put(ACT_VIDEO, VideoListAct.class.getName());


    }
    /**
     *
     * @param context
     * @param action  bsapp://action?key1=value1&key2=value2
     */
    public static void open(Context context, String action) {
        context.startActivity(getIntent(context, action));
    }

    /**
     * 使用场景
     *  登录成功 需要跳转到指定activity
     *
     * @param context
     * @param clazz 当前activity
     * @param callBackAct 跳转activity 自定义的缩写名称
     * @param stringMap
     * @return
     */
    public static Intent initAction (Context context, Class clazz, String callBackAct, Map<String, String> stringMap) {
        StringBuffer stringBuffer = null;
        try {
            //     bsapp://action?key1=value1&key2=value2
            stringBuffer = new StringBuffer();
            stringBuffer.append(SCHEME);
            stringBuffer.append(callBackAct);
            stringBuffer.append("?");
            Iterator<String> keys = stringMap.keySet().iterator();
            int i = 0;
            while (keys.hasNext()) {
                if (i > 0)
                    stringBuffer.append("&");
                String key = keys.next();
                stringBuffer.append(key);
                stringBuffer.append("=");
                stringBuffer.append(stringMap.get(key));
                i++;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        Intent intent = new Intent();
        intent.setClass(context, clazz);
        intent.putExtra(KEY_ACTION, stringBuffer.toString());
        return intent;
    }

    /**
     *  if (clsAction.equals(ACT_MAIN))
     intent.putExtra(IS_GOHOME, false);//首页不再需要返回首页
     else
     intent.putExtra(IS_GOHOME, true);//返回首页
     * @param context
     * @param action
     * @return
     */
    public static Intent getIntent (Context context, String action) {
        try {
//     bsapp://action?key1=value1&key2=value2
            if (action == null)
                return null;
            if (action.trim().length() == 0)
                return null;
            action = action.trim();

            if (!action.startsWith(SCHEME))
                return null;
            action = StringUtil.replace(action, SCHEME, "");
            action = action.replace(KEY_ACTION + "=", "");

            if (action.contains("?")) {
                String[] strs = action.split("\\?");
                Intent intent = new Intent();
                String clsAction = strs[0].trim();

                if (activityMap.containsKey(clsAction)) {
//                    if (clsAction.equals(ACT_MAIN))
//                        intent.putExtra(IS_GOHOME, false);//首页不再需要返回首页  首页没打开的情况
//                    else
//                        intent.putExtra(IS_GOHOME, true);//返回首页
                    intent.setClassName(context, activityMap.get(clsAction));
                } else {
                    return null;
                }

                if (strs.length > 1) {
                    String[] params = strs[1].split("&");
                    for (int i = 0; i < params.length; i++) {
                        String str = params[i].trim();
                        String[] temp = str.split("=");
                        String key = "";
                        if (temp.length > 0)
                            key = temp[0].trim();
                        String value = "";
                        if (temp.length > 1)
                            value = temp[1].trim();
                        value = ConvertUtil.NVL(value, "");
                        if (key.equalsIgnoreCase("url") && value.length() > 0)
                            value = URLDecoder.decode(value, "utf-8");
                        if (key != null && key.trim().length() > 0)
                            intent.putExtra(key, value);
                    }
                }

                return intent;
            } else {
                Intent intent = new Intent();
                if (activityMap.containsKey(action)) {
                    String clsAction = activityMap.get(action);
                    if (activityMap.containsKey(action)) {
//                        if (clsAction.equals(ACT_MAIN))
//                            intent.putExtra(IS_GOHOME, false);//首页不再需要返回首页
//                        else
//                            intent.putExtra(IS_GOHOME, true);//返回首页
                    }
                    intent.setClassName(context, clsAction);
                }
                return intent;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将封装的action解析成对象
     * @param context
     * @param action  jingling://action?title=hh&id=123
     * @return
     */
    public static ActionObj parseActionObj (Context context, String action) {
        ActionObj actionObj = new ActionObj();
        try {
            if (action == null)
                return null;
            if (action.trim().length() == 0)
                return null;
            action = action.trim();

            if (!action.startsWith(SCHEME))
                return null;
            action = StringUtil.replace(action, SCHEME, "");
            //特殊情况容错处理
            action = action.replace(KEY_ACTION + "=", "");

            if (action.contains("?")) {
                String[] strs = action.split("\\?");
                actionObj.setAction(strs[0].trim());

                if (strs.length > 1) {
                    String[] params = strs[1].split("&");
                    for (int i = 0; i < params.length; i++) {
                        String str = params[i].trim();
                        String[] temp = str.split("=");
                        String key = "";
                        if (temp.length > 0)
                            key = temp[0].trim();
                        String value = "";
                        if (temp.length > 1)
                            value = temp[1].trim();
                        value = ConvertUtil.NVL(value, "");
                        if (key.equalsIgnoreCase("url") && value.length() > 0)
                            value = URLDecoder.decode(value, "utf-8");
                        if (key != null && key.trim().length() > 0)
                            actionObj.getParamMap().put(key, value);
                    }
                }
            } else {
                actionObj.setAction(action);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return actionObj;
    }


    public static boolean isGoHome (Context context, Intent intent) {
        return intent.getBooleanExtra(IS_GOHOME, false);
    }
}
